package willevaluate.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import willevaluate.adapter.OrderCollegeAdapter;
import willevaluate.bean.OrderCollegeBean;
import common.BaseActivity;
import common.Constant;
import https.InterNetController;

/**
 * Created by Administrator on 2017/4/18.
 * 可以排序的评估列表
 */
@ContentView(R.layout.evaluate_order)
public class EvaluateOrder extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.common_back)
    private ImageView common_back;
    @ViewInject(R.id.common_title)
    private TextView common_title;
    @ViewInject(R.id.evaluate_order_lv)
    private ListView evaluate_order_lv;

    @ViewInject(R.id.evaluate_order_item1_layout)
    private RelativeLayout evaluate_order_item1_layout;
    @ViewInject(R.id.evaluate_order_item2_layout)
    private RelativeLayout evaluate_order_item2_layout;
    @ViewInject(R.id.evaluate_order_item3_layout)
    private RelativeLayout evaluate_order_item3_layout;
    @ViewInject(R.id.evaluate_order_item1)
    private TextView evaluate_order_item1;
    @ViewInject(R.id.evaluate_order_item2)
    private TextView evaluate_order_item2;
    @ViewInject(R.id.evaluate_order_item3)
    private TextView evaluate_order_item3;

    private OrderCollegeAdapter orderCollegeAdapter;
    private ArrayList<OrderCollegeBean.CollegeListBean> arrayList;

    private final String ORDER1 = "考研率";
    private final String ORDER2 = "男女比例";
    private final String ORDER3 = "就业竞争";
    private final String ORDER_UP = "升序";
    private final String ORDER_DOWN = "降序";


    private final int ORDER1_UP = 0;
    private final int ORDER1_DOWN = 1;
    private final int ORDER2_UP = 2;
    private final int ORDER2_DOWN = 3;
    private final int ORDER3_UP = 4;
    private final int ORDER3_DOWN = 5;
    private int current = -1;//点前选中第几个什么顺序

    private int chosed_color;
    private int unchosed_color;
    private Drawable evaluate_up_ima;
    private Drawable evaluate_no_ima;
    private Drawable evaluate_down_ima;


    private String status;//稳妥,保底或者冲刺
    private String count;
    private String type;
    private String rank;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    Gson gson = new Gson();
                    OrderCollegeBean orderCollegeBean = gson.fromJson((String) msg.obj, OrderCollegeBean.class);
                    if (orderCollegeBean != null) {
                        List<OrderCollegeBean.CollegeListBean> collegeList = orderCollegeBean.getCollegeList();
                        if (collegeList != null) {
                            if (collegeList.size() > 0) {
                                arrayList.clear();
                                for (int i = 0; i < collegeList.size(); i++) {
                                    arrayList.add(collegeList.get(i));
                                }
                            }
                            orderCollegeAdapter.notifyDataSetChanged();
                            if (!evaluate_order_lv.isStackFromBottom()) {
                                evaluate_order_lv.setStackFromBottom(true);
                            }
                            evaluate_order_lv.setStackFromBottom(false);
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        status = getIntent().getStringExtra("status");
        count = getIntent().getStringExtra("count");
        type = getIntent().getStringExtra("type");
        rank = getIntent().getStringExtra("rank");
        initView();
        getDataFromInterNet(true);
    }

    private void initView() {
        common_back.setOnClickListener(this);
        common_title.setText(status + "录取院校");
        evaluate_order_item1_layout.setOnClickListener(this);
        evaluate_order_item2_layout.setOnClickListener(this);
        evaluate_order_item3_layout.setOnClickListener(this);

        chosed_color = getResources().getColor(R.color.common_color);
        unchosed_color = getResources().getColor(R.color.text_color1);
        evaluate_up_ima = getResources().getDrawable(R.mipmap.evaluate_up_ima);
        evaluate_up_ima.setBounds(0, 0, evaluate_up_ima.getMinimumWidth(), evaluate_up_ima.getMinimumHeight());
        evaluate_down_ima = getResources().getDrawable(R.mipmap.evaluate_down_ima);
        evaluate_down_ima.setBounds(0, 0, evaluate_down_ima.getMinimumWidth(), evaluate_down_ima.getMinimumHeight());
        evaluate_no_ima = getResources().getDrawable(R.mipmap.evaluate_no_ima);
        evaluate_no_ima.setBounds(0, 0, evaluate_no_ima.getMinimumWidth(), evaluate_no_ima.getMinimumHeight());

        arrayList = new ArrayList<>();
        orderCollegeAdapter = new OrderCollegeAdapter(this, arrayList,count,type);
        evaluate_order_lv.setAdapter(orderCollegeAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_back:
                this.finish();
                break;
            case R.id.evaluate_order_item1_layout:
                if (current == -1) {
                    evaluate_order_item1.setTextColor(chosed_color);
                    evaluate_order_item2.setTextColor(unchosed_color);
                    evaluate_order_item3.setTextColor(unchosed_color);
                    evaluate_order_item1.setCompoundDrawables(null, null, evaluate_up_ima, null);
                    evaluate_order_item2.setCompoundDrawables(null, null, evaluate_no_ima, null);
                    evaluate_order_item3.setCompoundDrawables(null, null, evaluate_no_ima, null);
                    current = ORDER1_UP;
                } else {
                    switch (current) {
                        case ORDER1_UP:
                            //变为降序
                            evaluate_order_item1.setCompoundDrawables(null, null, evaluate_down_ima, null);
                            current = ORDER1_DOWN;
                            break;
                        case ORDER1_DOWN:
                            //变为升序
                            evaluate_order_item1.setCompoundDrawables(null, null, evaluate_up_ima, null);
                            current = ORDER1_UP;
                            break;
                        default:
                            evaluate_order_item1.setTextColor(chosed_color);
                            evaluate_order_item2.setTextColor(unchosed_color);
                            evaluate_order_item3.setTextColor(unchosed_color);
                            evaluate_order_item1.setCompoundDrawables(null, null, evaluate_up_ima, null);
                            evaluate_order_item2.setCompoundDrawables(null, null, evaluate_no_ima, null);
                            evaluate_order_item3.setCompoundDrawables(null, null, evaluate_no_ima, null);
                            current = ORDER1_UP;
                            break;
                    }
                }
                break;
            case R.id.evaluate_order_item2_layout:
                if (current == -1) {
                    evaluate_order_item1.setTextColor(unchosed_color);
                    evaluate_order_item2.setTextColor(chosed_color);
                    evaluate_order_item3.setTextColor(unchosed_color);
                    evaluate_order_item1.setCompoundDrawables(null, null, evaluate_no_ima, null);
                    evaluate_order_item2.setCompoundDrawables(null, null, evaluate_up_ima, null);
                    evaluate_order_item3.setCompoundDrawables(null, null, evaluate_no_ima, null);
                    current = ORDER2_UP;
                } else {
                    switch (current) {
                        case ORDER2_UP:
                            //变为降序
                            evaluate_order_item2.setCompoundDrawables(null, null, evaluate_down_ima, null);
                            current = ORDER2_DOWN;
                            break;
                        case ORDER2_DOWN:
                            //变为升序
                            evaluate_order_item2.setCompoundDrawables(null, null, evaluate_up_ima, null);
                            current = ORDER2_UP;
                            break;
                        default:
                            evaluate_order_item1.setTextColor(unchosed_color);
                            evaluate_order_item2.setTextColor(chosed_color);
                            evaluate_order_item3.setTextColor(unchosed_color);
                            evaluate_order_item1.setCompoundDrawables(null, null, evaluate_no_ima, null);
                            evaluate_order_item2.setCompoundDrawables(null, null, evaluate_up_ima, null);
                            evaluate_order_item3.setCompoundDrawables(null, null, evaluate_no_ima, null);
                            current = ORDER2_UP;
                            break;
                    }
                }
                break;
            case R.id.evaluate_order_item3_layout:
                if (current == -1) {
                    evaluate_order_item1.setTextColor(unchosed_color);
                    evaluate_order_item2.setTextColor(unchosed_color);
                    evaluate_order_item3.setTextColor(chosed_color);
                    evaluate_order_item1.setCompoundDrawables(null, null, evaluate_no_ima, null);
                    evaluate_order_item2.setCompoundDrawables(null, null, evaluate_no_ima, null);
                    evaluate_order_item3.setCompoundDrawables(null, null, evaluate_up_ima, null);
                    current = ORDER3_UP;
                } else {
                    switch (current) {
                        case ORDER3_UP:
                            //变为降序
                            evaluate_order_item3.setCompoundDrawables(null, null, evaluate_down_ima, null);
                            current = ORDER3_DOWN;
                            break;
                        case ORDER3_DOWN:
                            //变为升序
                            evaluate_order_item3.setCompoundDrawables(null, null, evaluate_up_ima, null);
                            current = ORDER3_UP;
                            break;
                        default:
                            evaluate_order_item1.setTextColor(unchosed_color);
                            evaluate_order_item2.setTextColor(unchosed_color);
                            evaluate_order_item3.setTextColor(chosed_color);
                            evaluate_order_item1.setCompoundDrawables(null, null, evaluate_no_ima, null);
                            evaluate_order_item2.setCompoundDrawables(null, null, evaluate_no_ima, null);
                            evaluate_order_item3.setCompoundDrawables(null, null, evaluate_up_ima, null);
                            current = ORDER3_UP;
                            break;
                    }
                }
                break;
            default:
                break;
        }
        getDataFromInterNet(false);
    }

    private void getDataFromInterNet(boolean waitingDialog) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("type", type);
        hashMap.put("rank", rank);
        hashMap.put("status", status);
        hashMap.put("count", count);
        if (current != -1) {
            switch (current) {
                case ORDER1_UP:
                    hashMap.put("order", ORDER1);
                    hashMap.put("order_status", ORDER_UP);
                    break;
                case ORDER1_DOWN:
                    hashMap.put("order", ORDER1);
                    hashMap.put("order_status", ORDER_DOWN);
                    break;
                case ORDER2_UP:
                    hashMap.put("order", ORDER2);
                    hashMap.put("order_status", ORDER_UP);
                    break;
                case ORDER2_DOWN:
                    hashMap.put("order", ORDER2);
                    hashMap.put("order_status", ORDER_DOWN);
                    break;
                case ORDER3_UP:
                    hashMap.put("order", ORDER3);
                    hashMap.put("order_status", ORDER_UP);
                    break;
                case ORDER3_DOWN:
                    hashMap.put("order", ORDER3);
                    hashMap.put("order_status", ORDER_DOWN);
                    break;
                default:
                    hashMap.put("order", ORDER1);
                    hashMap.put("order_status", ORDER_UP);
                    break;
            }
        }
        new InterNetController(this, Constant.EVALUTELIST, handler, hashMap, 0,waitingDialog);
    }


}
